package com.kerrykidz.sms.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sun.swing.StringUIClientPropertyKey;

import com.kerrykidz.sms.dao.mapper.SmsBatchEntityMapper;
import com.kerrykidz.sms.dao.mapper.SmsContEntityMapper;
import com.kerrykidz.sms.dao.mapper.SmsOutBoxAndBatchCondEntityMapper;
import com.kerrykidz.sms.dao.model.SmsBatchCondEntity;
import com.kerrykidz.sms.dao.model.SmsBatchEntity;
import com.kerrykidz.sms.dao.model.SmsContEntity;
import com.kerrykidz.sms.dao.model.SmsOutBoxEntity;
import com.kerrykidz.sms.service.IBatchService;
import com.kerrykidz.sms.util.BatchCondUtil;
import com.rdp.framework.exception.RDPException;
import com.rdp.util.GenerationUUID;
/***
 * 批量发送短信消息
 * @ClassName: BatchServiceImpl 
 * @description: 大致描述该类的用途，内容简短易懂 
 * @author: wolf-yansl 
 * @date: 2014年9月25日 上午11:02:31 
 * @version: V1.0
 *
 */
@Service
public class BatchServiceImpl implements IBatchService {
	
	@Autowired
	private SmsBatchEntityMapper smsBatchMapper;
	@Autowired
	private SmsContEntityMapper smsContMapper;
	@Autowired
	private SmsOutBoxAndBatchCondEntityMapper smsOutBoxAndBatchCondMapper;
	@Override
	public List<SmsBatchEntity> findSmsBatchEntity(SmsBatchEntity entity)throws RDPException {
		entity.setIspaging(true);
		return smsBatchMapper.getSmsBatchEntity(entity);
	}
	public SmsBatchEntity findBySmsBatchId(String smsBatchId) throws RDPException{
		SmsBatchEntity paramEntity = new SmsBatchEntity();
		paramEntity.setPkBatchId(smsBatchId);
		
		List<SmsBatchEntity> listOneBatch = smsBatchMapper.getSmsBatchEntity(paramEntity);
		if(listOneBatch != null && listOneBatch.size() == 1){
			return listOneBatch.get(0);
		}
		return null;
		
	}
	@Override
	public SmsBatchEntity addSmsBatchEntity(SmsBatchEntity entity) throws RDPException {
		
		/**保存发送模板*/
		SmsContEntity contTemp = new SmsContEntity(); 
		contTemp.setPkSmsContId(GenerationUUID.getGenerationUUID());
		contTemp.setTmplFlag("0");
		contTemp.setReserveFlag(SmsContEntity.RESERVE_FALG_FALSE);
		contTemp.setContText(entity.getContext());
		contTemp.setFkBranId(entity.getFkBranId());
		contTemp.setModiBy(entity.getModiBy());
		smsContMapper.insertSmsContEntity(contTemp);
		
		entity.setFkSmscontId(contTemp.getPkSmsContId());
		smsBatchMapper.insertSmsBatchEntity(entity);
		
		/**处理发送条件  存在发送 判断*/
		batchWhere(entity);
		
		return entity;
		
	}
	@Override
	public SmsBatchEntity updateSmsBatchEntity(SmsBatchEntity entity) throws RDPException {
		String batchId = entity.getPkBatchId();
		SmsBatchEntity paramEntity = new SmsBatchEntity();
		paramEntity.setPkBatchId(batchId);
		
		List<SmsBatchEntity> listOneBatch = smsBatchMapper.getSmsBatchEntity(paramEntity);
		if(listOneBatch != null && listOneBatch.size() == 1){
			paramEntity = listOneBatch.get(0);
			
			/**修改模板内容*/
			String fkContId = paramEntity.getFkSmscontId();
			
			SmsContEntity contEntity = new SmsContEntity();
			contEntity.setPkSmsContId(fkContId);
			List<SmsContEntity> contList = smsContMapper.getSmsContEntity(contEntity);
			
			if(contList != null && contList.size() == 1){
				contEntity = contList.get(0);
				contEntity.setContText(entity.getContext());
				smsContMapper.updateSmsContEntity(contEntity);
			}
			entity.setFkSmscontId(fkContId);
			BeanUtils.copyProperties(entity, paramEntity);
			smsBatchMapper.updateSmsBatchEntity(paramEntity);
			
			/**取消所有关联手机  和  模板*/
			String srcType = entity.getSrcType();
			smsOutBoxAndBatchCondMapper.deleteSmsBatchCondEntity(paramEntity.getPkBatchId());
			if(StringUtils.isNotBlank(srcType) && srcType.equals("1")){
				smsOutBoxAndBatchCondMapper.deleteSmsOutBox(paramEntity.getPkBatchId(),null,null);
			}else if(StringUtils.isNotBlank(srcType) && srcType.equals("2")){
				if(paramEntity.getPhoneList() != null && paramEntity.getPhoneList().size() > 0){
					smsOutBoxAndBatchCondMapper.deleteSmsOutBox(paramEntity.getPkBatchId(),paramEntity.getPhoneList(),null);
				}
			}
			
			batchWhere(paramEntity);
			return entity;
		}
		return null;
		
		
	}
	@Override
	public void updateCloseSmsBatchEntity(SmsBatchEntity entity)throws RDPException {
		String batchId = entity.getPkBatchId();
		SmsBatchEntity paramEntity = new SmsBatchEntity();
		paramEntity.setPkBatchId(batchId);
		
		List<SmsBatchEntity> listOneBatch = smsBatchMapper.getSmsBatchEntity(paramEntity);
		if(listOneBatch != null && listOneBatch.size() == 1){
			paramEntity = listOneBatch.get(0);
			paramEntity.setStatus(SmsBatchEntity.STATUS_GB);
			paramEntity.setModiBy(entity.getModiBy());
			smsBatchMapper.updateSmsBatchEntity(paramEntity);
		}
	}
	@Override
	public void deleteSmsBatchEntity(SmsBatchEntity entity) throws RDPException {
		entity = findBySmsBatchId(entity.getPkBatchId());
		if(entity != null ){
			entity.setStatus(SmsBatchEntity.STATUS_SC);
			smsBatchMapper.deleteSmsBatchEntity(entity);
			
			/**取消所有关联手机  和  模板
			smsOutBoxAndBatchCondMapper.deleteSmsBatchCondEntity(entity.getPkBatchId());
			smsOutBoxAndBatchCondMapper.deleteSmsOutBox(entity.getPkBatchId(),null,null);*/
		}
		
	}
	@Override
	public List<SmsContEntity> findSmsContEntity(SmsContEntity entity)throws RDPException {
		return smsContMapper.getSmsContEntity(entity);
	}
	@Override
	public List<SmsOutBoxEntity> findSmsOutBoxEntity(SmsOutBoxEntity entity)throws RDPException {
		return smsOutBoxAndBatchCondMapper.getSmsOutBoxEntity(entity);
	}
	@Override
	public List<SmsBatchCondEntity> findSmsBatchCondEntity(	SmsBatchCondEntity entity) throws RDPException {
		return smsOutBoxAndBatchCondMapper.getSmsBatchCondEntity(entity);
	}
	
	
	private void batchWhere(SmsBatchEntity entity) throws RDPException{
		String batchId = entity.getPkBatchId();
		/**处理模板条件*/
		List<SmsBatchCondEntity> condList = entity.getCondList();
		if(condList != null && condList.size() > 0 && entity.getSrcType() != null && entity.getSrcType().equals("1") ){
			for(SmsBatchCondEntity condEntity : condList){
				condEntity.setSmsBatchcondId(GenerationUUID.getGenerationUUID());
				condEntity.setFkBatchId(batchId);
				smsOutBoxAndBatchCondMapper.insertSmsBatchCondEntity(condEntity);
			}
		}
		
		/**处理手工录入电话*/
		List<String> phoneList = entity.getPhoneList();
		if(phoneList != null && phoneList.size() > 0 && entity.getSrcType() != null && entity.getSrcType().equals("2")){
			for(String str : phoneList){
				SmsOutBoxEntity smsOutBox = new SmsOutBoxEntity();
				smsOutBox.setPkSmsId(GenerationUUID.getGenerationUUID());
				smsOutBox.setFkBatchId(batchId);
				smsOutBox.setFkBranId(entity.getFkBranId());
				smsOutBox.setDestPhone(str);
				smsOutBox.setFkSmsContId(entity.getFkSmscontId());
				smsOutBox.setSendResult(SmsOutBoxEntity.UNSEND);
				
				smsOutBoxAndBatchCondMapper.insertSmsOutBox(smsOutBox);
			}
		}
		/**如果是提交类型初始化数据*/
		if(entity.getStatus() != null && entity.getStatus().equals("2")){
			SmsBatchCondEntity condEntity = new SmsBatchCondEntity();
			condEntity.setFkBatchId(batchId);
			List<String> condPhoneList = findSmsBatchCondPhoneList(condEntity,entity.getCondList());
			if(condPhoneList.size() > 0){
				for(String str : condPhoneList){
					SmsOutBoxEntity smsOutBox = new SmsOutBoxEntity();
					smsOutBox.setPkSmsId(GenerationUUID.getGenerationUUID());
					smsOutBox.setFkBatchId(batchId);
					smsOutBox.setFkBranId(entity.getFkBranId());
					smsOutBox.setDestPhone(str);
					smsOutBox.setFkSmsContId(entity.getFkSmscontId());
					smsOutBox.setSendResult(SmsOutBoxEntity.UNSEND);
					
					smsOutBoxAndBatchCondMapper.insertSmsOutBox(smsOutBox);
				}
			}
			
		}
	}
	@Override
	public SmsContEntity addSmsContEntity(SmsContEntity entity) throws RDPException {
		entity.setPkSmsContId(GenerationUUID.getGenerationUUID());
		entity.setReserveFlag(SmsContEntity.RESERVE_FALG_FALSE);
		smsContMapper.insertSmsContEntity(entity);
		return entity;
	}
	@Override
	public void updateSmsContEntity(SmsContEntity entity) throws RDPException {
		String smsContId = entity.getPkSmsContId();
		
		SmsContEntity  pramaContEntity = new SmsContEntity();
		pramaContEntity.setPkSmsContId(smsContId);
		List<SmsContEntity> contList = smsContMapper.getSmsContEntity(pramaContEntity);
		if(contList != null && contList.size() == 1){
			pramaContEntity = contList.get(0);
			BeanUtils.copyProperties(entity, pramaContEntity);
			smsContMapper.updateSmsContEntity(pramaContEntity);
		}
		
	}
	@Override
	public void deleteSmsContEntity(SmsContEntity entity) throws RDPException {
		smsContMapper.deleteSmsContEntity(entity);
	}
	@Override
	public void deleteSmsOutBox(String fkBatchId, String pkSmsId)throws RDPException {
		List<String> boxList = new ArrayList<String>();
		boxList.add(pkSmsId);
		smsOutBoxAndBatchCondMapper.deleteSmsOutBox(fkBatchId, null,pkSmsId);
	}
	@Override
	public List<String> findSmsBatchCondPhoneList(SmsBatchCondEntity entity,List<SmsBatchCondEntity> entityList) throws RDPException {
		if(entityList == null || entityList.size() == 0){
			entityList = smsOutBoxAndBatchCondMapper.getSmsBatchCondEntity(entity);
		}
		
		
		List<String> resultList = new ArrayList<String>();
		Map<String,Object> map = new HashMap<String,Object>();
		
		BatchCondUtil.smsBatchCondWhereMap(entityList, map);
		if(map.size() > 0){
			resultList = smsBatchMapper.getByBatchCondWherePhoneList(map);
		}
		return resultList;
	}
	@Override
	public int findByBatchCondWherePhoneCount(SmsBatchCondEntity entity)throws RDPException {
		int resultCount = 0; 
		List<SmsBatchCondEntity> entityList = smsOutBoxAndBatchCondMapper.getSmsBatchCondEntity(entity);
		
		Map<String,Object> map = new HashMap<String,Object>();
		BatchCondUtil.smsBatchCondWhereMap(entityList, map);
		if(map.size() > 0){
			resultCount = smsBatchMapper.getByBatchCondWherePhoneCount(map);
		}
		return resultCount;
	}
	@Override
	public List<SmsBatchEntity> findHisBatchWhere(SmsBatchEntity entity) throws RDPException {
		entity.setIspaging(true);
		return smsBatchMapper.getHisBatchWhere(entity);
	}
	
	

}
